import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  ParseIntPipe,
  HttpCode,
  HttpStatus,
  ValidationPipe,
} from '@nestjs/common';
import { DicService } from '@/modules/dic/dic.service';
import { Public } from '@/modules/auth/decorators/public.decorator';
import { Dic } from '@/entity/dic/dic.entity';

@Controller('/api/dic')
export class DicController {
  constructor(private readonly DicService: DicService) {}
  @Post('dicAdd')
  async dicAdd(@Body() postParams: any): Promise<any> {
    try {
      const res = await this.DicService.insert(postParams);
      if (res) {
        return {
          code: 200,
          data: null,
          message: 'ok',
        };
      } else {
        return {
          code: 1000,
          data: null,
          message: 'error',
        };
      }
    } catch (error) {
      return {
        code: 500,
        message: error,
      };
    }
  }

  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('queryDic')
  async queryDic(
    @Query('dic_type', new ValidationPipe({ transform: true }))
    dic_type: string,
  ): Promise<{ code: number; data: Dic[]; message: string }> {
    try {
      console.log('参数', dic_type);
      const res = await this.DicService.findDic(dic_type);
      return {
        code: res ? 200 : 1000,
        data: res ? res : null,
        message: res ? 'ok' : res,
      };
      return res;
    } catch (error) {
      return {
        code: 500,
        data: [],
        message: error,
      };
    }
  }

  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('findAllDic')
  async findAllDic(): Promise<{
    code: number;
    data: Array<Dic>;
    message: string;
  }> {
    try {
      const res = await this.DicService.findAll();
      return {
        code: res ? 200 : 1000,
        data: res ? res : [],
        message: res ? 'ok' : '',
      };
    } catch (error) {
      return {
        code: 500,
        data: [],
        message: error,
      };
    }
  }
}
